<?php
namespace app\api\model;

use think\Model;
use think\Db;
use function think\insert;

/**
 *
 * @author sj
 *         我的账户模型
 */
class Myaccount extends Model
{

    /**
     *
     * @param int $user_id
     *            用户ID
     * @return array $res 用户的信息列表 点击在控制器
     */
    public function selectMyaccount($user_id)
    {
        try {
            $res = Db::name('my_account')->where(array(
                'user_id' => $user_id
            ))->find();
            if (empty($res)) {
                return 0;
            } else {
                return $res;
            }
        } catch (\think\Exception $e) {
            trace('select error' . $e->getMessage());
        }
    }

    /**
     *
     * @param array $data
     *            添加账号余额冻结账户
     *            缴纳押金
     *            关于上面时候添加数据库的押金记录
     *            肯定是充值成功后
     *            押金记录添加
     *            这里单独的是一个单独的模块
     *            
     */
    public function Paydeposit($data)
    {
        $id = $this->save($data);
        if ($id) {
            return $id;
        } else {
            return 0;
        }
    }

    /**
     * 这里是确认预约后
     * 用户账号余额更新
     */
    public function updateAccount($data)
    {
        try {
            $id = $this->where(array(
                'user_id' => $data['user_id']
            ))->update(array(
                'dongjie' => $data['dongjie']
            ));
            if ($id) {
                return $id;
            } else {
                return 0;
            }
        } catch (\think\Exception $e) {
            die('unkown exception' . $e->getMessage());
        }
    }

    /**
     * 我的充值记录
     */
    public function recharge($user_id)
    {
        $res = Db::query("SELECT
re.create_time,
re.money
FROM
gx_recharge re
WHERE re.user_id=?
AND re.`status`=1", array(
            $user_id
        ));
        if (! empty($res)) {
            return $res;
        } else {
            return 0;
        }
    }

    /**
     * 我的提现记录
     */
    public function withdrawals($user_id)
    {
        $res = Db::query("SELECT
	w.create_time,
	w.money
FROM
	gx_withdrawals w
WHERE
	w.user_id = ?
AND w.`status` = 1", array(
            $user_id
        ));
        if(!empty($res)){
            return $res;
        }else{
            return 0;
        }
    }
    /**
     * 我的支付记录* 
     */
    public function pay_info($user_id){
       $res= Db::query("SELECT
	gy.order_id,
	gy.riqi,
	gy.appoint_time,
	pay.pay_money,
	p.designation,
    pay.pay_time
FROM
	gx_pay_info pay,
	gx_appointment_gym gy,
	gx_order o,
	gx_place p
WHERE
	pay.order_id = o.id
AND gy.order_id = pay.order_id
AND o.`status` = 200
AND gy.user_id = ?
AND p.id = gy.place_id",array($user_id));
      
       if(!empty($res)){
           foreach ($res as $key=>$va){
               $res[$key]['count']=array_to_fe($va['appoint_time']);
               
           }
           return $res;
       }else{
           return 0;
       }
    }
    /**
     * 我的取消押金扣除记录
     */
    public function weiyue($user_id){
       $res= Db::query("SELECT
gy.riqi,
gy.appoint_time,
o.order_number,
o.cancle_time,
gy.dongjie,
p.designation
FROM
	gx_appointment_gym gy,
	gx_order o,
  gx_place p
WHERE gy.order_id=o.id
AND gy.user_id=?
AND o.`status`=10
AND p.id=gy.place_id",array($user_id));
       if(!empty($res)){
          foreach ($res as $key=>$va){
              $res[$key]['count']=array_to_fe($va['appoint_time']);
             // $res[$key]['veninfo']=
          }
          return $res;
       }else{
           return 0;
       }
    }
}